// 引入 SDK
// const crawlab = require('crawlab-sdk')
const crawlab = {
  saveItem: console.log,
  close: () => console.log('close')
}

const axios = require('axios')
const cheerio = require('cheerio')
const schoolName = '北京大学'
const baseURL = 'https://pkunews.pku.edu.cn/xwzh/'
// 这是一个结果，需要为 object 类型
// const result = {name: 'crawlab'};

;(async () => {
  crawPage(`${baseURL}index.htm`, rule)
  // 调用保存结果方法
  // await crawlab.saveItem(result);

  // // 关闭数据库连接，否则程序不会结束
})()

async function crawPage(url, func) {
  const { data: html } = await axios(url)
  const $ = cheerio.load(html)
  func && func($)
}

async function rule ($) {
  const res = $('.imgHover')
  for(let i = 0; i < res.length; i++) {
    const item = $(res[i])
    const href = baseURL + item.find('.item-txt a').attr('href')
    const title = item.find('.item-txt a').text()
    const date = item.find('.item-date').text()
    await crawlab.saveItem({
      time: date,
      url: href,
      title,
      schoolName
    })
  }
  const next = $('a.icon-arrow1').attr('href')
  // const next = $('.icon-arrow1')
  crawPage(`${baseURL}${next}`, rule)
  if (next) {
    crawPage(`${baseURL}${next}`, rule)
  } else {
    await crawlab.close();
  }
}

// https://news.tsinghua.edu.cn/info/1003/78759.htm
// https://news.tsinghua.edu.cn/zhxw/info/1003/78795.htm